// pages/notifyList/index.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    status: 1,
    edit: 'no',
    list: [],
    addNotify: '创建通知'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({ status: options.status, edit: options.edit });
    if(options.status == 2) {
      wx.setNavigationBarTitle({
        title: '部门职责公告',
      });
      this.setData({ addNotify: '创建公告' })
    }
    wx.hideShareMenu();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getList();
    if(this.data.status == 1) {
      this.readNotice();
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },

  readNotice() {
    wx.request({
      url: `${app.globalData.baseUrl}/api/notice/readNotice`,
      method: "GET",
      header: {
        'token': wx.getStorageSync('token'),
        "Content-Type": "application/json"
      },
      success: (res) => {
        if (res.data.code == 0) {
          console.log(res.data)
        }
      }
    })
  },

  getList() {
    wx.request({
      url: `${app.globalData.baseUrl}/api/notice/selNoticeList?status=${this.data.status}`,
      method: "GET",
      header: {
        'token': wx.getStorageSync('token'),
        "Content-Type": "application/json"
      },
      success: (res) => {
        if (res.data.code == 0) {
          this.setData({ list: res.data.rows });
        }
      }
    })
  },

  goDetail(e) {
    wx.navigateTo({
      url: `/pages/notifyDetail/index?status=${this.data.status}&id=${e.currentTarget.dataset.id}`,
    });
  },

  addNotify() {
    wx.navigateTo({
      url: `/pages/notifications/index?status=${this.data.status}&edit=${this.data.edit}`,
    });
  },

  delete(e) {
    const _that = this;
    wx.showModal({
      content: '确认删除吗？',
      success: function (sm) {
        if (sm.confirm) {
          wx.request({
            url: `${app.globalData.baseUrl}/api/notice/delNoticeId?id=${e.currentTarget.dataset.id}`,
            method: "GET",
            header: {
              'token': wx.getStorageSync('token'),
              "Content-Type": "application/json"
            },
            success: (res) => {
              if (res.data.code === 0) {
                _that.getList();
                wx.showToast({
                  title: '操作成功',
                });
              }
            }
          })
        }
      }
    });
  }
})